package cn.iocoder.yudao.module.wms.service.index;

import cn.iocoder.yudao.module.wms.controller.admin.index.vo.BaseDataVO;
import cn.iocoder.yudao.module.wms.dal.mysql.index.IndexMapper;
import cn.iocoder.yudao.module.wms.service.rdrecord01.Rdrecord01Service;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

@Service
@Validated
public class IndexServiceImpl implements IndexService {
    @Resource
    IndexMapper indexMapper;
    @Override
    public List<BaseDataVO> getStock() {
        return indexMapper.getStock();
    }

    @Override
    public BaseDataVO getPomain() {
        return indexMapper.getPomain().setName("采购订单");
    }

    @Override
    public BaseDataVO getRdrecord01() {
        return indexMapper.getRdrecord01().setName("采购入库");
    }

    @Override
    public BaseDataVO getRdrecord06() {
        return indexMapper.getRdrecord06().setName("调拨入库");
    }

    @Override
    public BaseDataVO getRdrecord07() {
        return indexMapper.getRdrecord07().setName("调拨出库");
    }

    @Override
    public BaseDataVO getRdrecord08() {
        return indexMapper.getRdrecord08().setName("其他入库");
    }

    @Override
    public BaseDataVO getRdrecord09() {
        return indexMapper.getRdrecord09().setName("其他出库");
    }

    @Override
    public BaseDataVO getRdrecord10() {
        return indexMapper.getRdrecord10().setName("生产入库");
    }

    @Override
    public BaseDataVO getRdrecord11() {
        return indexMapper.getRdrecord11().setName("材料出库");
    }

    @Override
    public BaseDataVO getRdrecord32() {
        return indexMapper.getRdrecord32().setName("销售出库");
    }

    @Override
    public BaseDataVO getSaleOrder() {
        return indexMapper.getSaleOrder().setName("销售订单");
    }

    @Override
    public BaseDataVO getProductionOrder() {
        return indexMapper.getProductionOrder().setName("生产订单");
    }

    @Override
    public List<BaseDataVO> getOrderList() {
        List<BaseDataVO> list=new ArrayList<>();
        list.add(getRdrecord01());
        list.add(getRdrecord06());
        list.add(getRdrecord07());
        list.add(getRdrecord08());
        list.add(getRdrecord09());
        list.add(getRdrecord10());
        list.add(getRdrecord11());
        list.add(getRdrecord32());
        list.add(getProductionOrder());
        list.add(getSaleOrder());
        list.add(getPomain());

        return list;
    }
    @Override
    public List<BaseDataVO> getLineOrder(String type)
    {
        try{
            //获取12个月月份
            List<String> months = new ArrayList<>();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
            LocalDate now = LocalDate.now();

            // 从当前月份开始，逐月向前推12个月
            for (int i = 0; i < 12; i++) {
                LocalDate date = now.minus(i, ChronoUnit.MONTHS);
                months.add(date.format(formatter));
            }

            // 如果需要按时间顺序排列，可以反转列表
            java.util.Collections.reverse(months);

            List<BaseDataVO> list=new ArrayList<>();
            for (String month:months)
            {
                DateTimeFormatter formatterString = DateTimeFormatter.ofPattern("yyyy-MM");
                YearMonth yearMonth = YearMonth.parse(month, formatterString);

                // 获取指定月份的第一天
                LocalDate firstDayOfThisMonth = yearMonth.atDay(1);

                // 获取下个月的 YearMonth 对象
                YearMonth nextMonth = yearMonth.plusMonths(1);

                // 获取下个月的第一天
                LocalDate firstDayOfNextMonth = nextMonth.atDay(1);

                int sum=indexMapper.getPomainMonth(firstDayOfThisMonth,firstDayOfNextMonth,type)+
                        indexMapper.getProductionOrderMonth(firstDayOfThisMonth,firstDayOfNextMonth,type)+
                        indexMapper.getSaleOrderMonth(firstDayOfThisMonth,firstDayOfNextMonth,type)+
                        indexMapper.getRdrecord01Month(firstDayOfThisMonth,firstDayOfNextMonth,type)+
                        indexMapper.getRdrecord06Month(firstDayOfThisMonth,firstDayOfNextMonth,type)+
                        indexMapper.getRdrecord07Month(firstDayOfThisMonth,firstDayOfNextMonth,type)+
                        indexMapper.getRdrecord08Month(firstDayOfThisMonth,firstDayOfNextMonth,type)+
                        indexMapper.getRdrecord09Month(firstDayOfThisMonth,firstDayOfNextMonth,type)+
                        indexMapper.getRdrecord10Month(firstDayOfThisMonth,firstDayOfNextMonth,type)+
                        indexMapper.getRdrecord11Month(firstDayOfThisMonth,firstDayOfNextMonth,type)+
                        indexMapper.getRdrecord32Month(firstDayOfThisMonth,firstDayOfNextMonth,type);
                BaseDataVO baseDataVO=new BaseDataVO();
                baseDataVO.setName(month);
                baseDataVO.setValue(String.valueOf(sum));
                list.add(baseDataVO);
            }
            return list;
        }catch (Exception e)
        {
            System.out.println(e.getMessage());

        }
        return new ArrayList<>();
    }


    @Override
    public int getOrderByCreat(String type,Long creator)
    {
        int sum=indexMapper.getPomainByCreator(creator,type)+
                indexMapper.getProductionOrderByCreator(creator,type)+
                indexMapper.getSaleOrderByCreator(creator,type)+
                indexMapper.getRdrecord01ByCreator(creator,type)+
                indexMapper.getRdrecord06ByCreator(creator,type)+
                indexMapper.getRdrecord07ByCreator(creator,type)+
                indexMapper.getRdrecord08ByCreator(creator,type)+
                indexMapper.getRdrecord09ByCreator(creator,type)+
                indexMapper.getRdrecord10ByCreator(creator,type)+
                indexMapper.getRdrecord11ByCreator(creator,type)+
                indexMapper.getRdrecord32ByCreator(creator,type);
        return sum;
    }


}
